package com.haozhen.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.NavigableMap;

/**
 * @author haozhen
 * @email haozh@ync1.com
 * @date 2020/10/21  18:15
 */
public class HbaseClientDemo {

    Connection conn = null;

    HBaseAdmin admin;

    @Before
    public void init() throws IOException {
        final Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","test1,test2");
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        conn =   ConnectionFactory.createConnection(configuration);
    }


//    @Test
    public void createTable() throws IOException {
        //获取hbaseAdmin 对象，用来创建表
        admin = (HBaseAdmin) conn.getAdmin();
        // 创建Htabledesc表描述器
        final HTableDescriptor student = new HTableDescriptor(TableName.valueOf("student"));
        // 指定列族
        student.addFamily(new HColumnDescriptor("info"));
        admin.createTable(student);
        System.out.println("student表创建成功");
    }

//    @Test
    public void putData() throws IOException {
        //需要获取一个table对象
        final Table student = conn.getTable(TableName.valueOf("student"));
        // 准备put对象
        final Put put = new Put(Bytes.toBytes("113"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("addr"), Bytes.toBytes("zhengz"));
        put.addColumn(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("lichao3"));
        student.put(put);
        System.out.println("student 插入数据成功！");
    }

    @Test
    public void deleteData() throws IOException {
        //需要获取一个table对象
        final Table student = conn.getTable(TableName.valueOf("student"));
        Delete delete = new Delete(Bytes.toBytes("113"));
        delete.addColumn(Bytes.toBytes("info"),Bytes.toBytes("addr"));
        NavigableMap<byte[], List<Cell>> familyCellMap = delete.getFamilyCellMap();
        familyCellMap.forEach((key,value)->{
            System.out.println(Bytes.toString(key));
            value.forEach(cell -> {
                System.out.println(Bytes.toString(cell.getValue()));
            });
        });
        student.delete(delete);
        System.out.println("删除一条数据");
    }

    public void getData() throws IOException {
        //准备table对象
        Table student = conn.getTable(TableName.valueOf("student"));

        //准备get对象
        final Get get = new Get(Bytes.toBytes("110"));
        //指定查询某个列族
        get.addFamily(Bytes.toBytes("info"));
        final Result result = student.get(get);
        final Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            byte[] bytes = CellUtil.cloneRow(cell);
            String rk = Bytes.toString(bytes);

            byte[] cFbytes = CellUtil.cloneFamily(cell);
            String cf = Bytes.toString(cFbytes);

            byte[] cbytes = CellUtil.cloneRow(cell);
            String c = Bytes.toString(cbytes);

            byte[] vbytes = CellUtil.cloneRow(cell);
            String value = Bytes.toString(vbytes);

            System.out.println("rowkey=:"+rk+
                    "    cf=: "+cf+
                    "   c=:"+c+
                    "   v=:"+value);
        }
    }

    public void scanData() throws IOException {
        //准备table对象
        Table student = conn.getTable(TableName.valueOf("student"));

        Scan scan = new Scan();
        //执行扫描
        ResultScanner scanner = student.getScanner(scan);
        for (Result result : scanner) {
            final Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                byte[] bytes = CellUtil.cloneRow(cell);
                String rk = Bytes.toString(bytes);

                byte[] cFbytes = CellUtil.cloneFamily(cell);
                String cf = Bytes.toString(cFbytes);

                byte[] cbytes = CellUtil.cloneRow(cell);
                String c = Bytes.toString(cbytes);

                byte[] vbytes = CellUtil.cloneRow(cell);
                String value = Bytes.toString(vbytes);

                System.out.println("rowkey=:"+rk+
                        "    cf=: "+cf+
                        "   c=:"+c+
                        "   v=:"+value);
            }
        }

    }

    public void scanStartEndDate() throws IOException {
        //准备table对象
        Table student = conn.getTable(TableName.valueOf("student"));

        final Scan scan= new Scan();

        scan.setStartRow(Bytes.toBytes("001"));
        scan.setStopRow(Bytes.toBytes("2"));

    }

    @After
    public void realse(){
        if(admin!=null){
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
